﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using Microsoft.WindowsMobile.PocketOutlook;
using System.Globalization;

namespace OutlookHub.Utils
{
    public static class TaskUtil
    {
        public static string ToDuration(this Task task)
        {
            StringBuilder sb = new StringBuilder();
            if (task.IsRecurring)
            {
                sb.Append("every ");
                TaskRecurrence tr = task.RecurrencePattern;
                switch (tr.RecurrenceType)
                {
                    case RecurrenceType.Daily:
                        sb.Append("day ");
                        break;

                    case RecurrenceType.Weekly:
                        sb.Append(ToString(tr.DaysOfWeekMask));
                        break;

                    case RecurrenceType.Monthly:
                        sb.Append(tr.DayOfMonth);
                        sb.Append(tr.DayOfMonth.ToPrefix());
                        sb.Append(" each month");
                        break;

                    case RecurrenceType.Yearly:
                        sb.Append(tr.DayOfMonth);
                        sb.Append(tr.DayOfMonth.ToPrefix());
                        sb.Append(" ");
                        sb.Append(tr.MonthOfYear.AsString());
                        break;

                    case RecurrenceType.MonthByNumber:
                        sb.Append(tr.Interval);
                        sb.Append(tr.Interval.ToPrefix());
                        sb.Append(" month");
                        break;

                    case RecurrenceType.YearByNumber:
                        sb.Append(tr.Interval);
                        sb.Append(tr.Interval.ToPrefix());
                        sb.Append(" year");
                        break;

                    case RecurrenceType.NoRecurrence: break;
                }
                if (!tr.PatternStartDate.IsNull())
                {
                    sb.Append(", starting at ");
                    sb.Append(tr.PatternStartDate.ToShortDateString());
                }
                if (!tr.NoEndDate)
                {
                    sb.Append(", ending on ");
                    sb.Append(tr.PatternEndDate.ToShortDateString());
                }

            }
            else
            {
                sb.Append(task.StartDate.AsString());
                if (!task.DueDate.IsNull())
                {
                    sb.Append(" - ");
                    sb.Append(task.DueDate.AsString());
                }
            }
            return sb.ToString();
        }

        public static string AsString(this DateTime date)
        {
            if (date.IsNull()) return string.Empty;
            return date.ToShortDateString();
        }

        public static bool IsNull(this DateTime date)
        {
            return date.ToOADate() == 949998.0d;
        }

        private static string ToPrefix(this int n)
        {
            switch (n)
            {
                case 1: return "st";
                case 2: return "nd";
                case 3: return "rd";
                default: return "th";
            }
        }

        private static string AsString(this Month month)
        {
            if (month == Month.None) return string.Empty;
            var monthNames = CultureInfo.CurrentUICulture.DateTimeFormat.MonthNames;
            return monthNames[(int)month];
        }

        private static string ToString(DaysOfWeek daysOfWeek)
        {
            StringBuilder sb = new StringBuilder();
            var dayNames = CultureInfo.CurrentUICulture.DateTimeFormat.DayNames;
            int i = 1;
            int n = 0;
            while (i <= 64)
            {
                DaysOfWeek day = (DaysOfWeek)i;
                if ((daysOfWeek & day) != 0)
                {
                    if (sb.Length > 0) sb.Append(", ");
                    sb.Append(dayNames[n]);
                }
                i *= 2;
                n++;
            }
            return sb.ToString();
        }

        public static IEnumerable<string> SplitByComma(this string list)
        {
            if (list == null) throw new ArgumentNullException("list");
            var items = list.Split(',');
            foreach (var item in items)
            {
                string s = item.Trim();
                if (s.Length > 0) yield return s;
            }
        }

        public static string AsCommaDelimitedString(this IEnumerable<string> list)
        {
            StringBuilder sb = new StringBuilder();
            foreach (var s in list)
            {
                if (sb.Length > 0) sb.Append(", ");
                sb.Append(s);
            }
            return sb.ToString();
        }


        public static void Copy(this Task task, Task copy)
        {
            task.Subject = copy.Subject;
            task.Body = copy.Body;
            task.StartDate = copy.StartDate;
            task.DueDate = copy.DueDate;
            task.ReminderSet = copy.ReminderSet;
            task.ReminderTime = copy.ReminderTime;
            task.Sensitivity = copy.Sensitivity;
            task.Complete = copy.Complete;
            task.Importance = copy.Importance;
        }
    }
}
